﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;

namespace EM.operation.paymentplan
{
    public partial class _default : System.Web.UI.Page
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(18, Authentication.RedirectionURLTypes.LoginPage);

            if (!IsPostBack)
            {
                FillCombo fc = new FillCombo();
                fc.IncomeType(ddlPAYMENTTYPE, 0, true, Convert.ToInt32(Session["WORKGROUPID"]));
            }

        }
        private DataTable GetData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";
            sSQL += @"SELECT PAYMENTPLANID,PP.PAYMENTTYPEID,AMOUNT,CC.CURRENCYID,DESCRIPTION,PLANNEDPAYMENTDATE,
                        PT.PAYMENTTYPENAME, CC.CURRENCYCODE
                        STATUS FROM TBLPAYMENTPLAN PP, TBLPAYMENTTYPE PT, TBLCURRENCY CC
                        WHERE PP.PAYMENTTYPEID = PT.PAYMENTTYPEID
                        AND PP.CURRENCYID = CC.CURRENCYID
                        ";

            if (ddlPAYMENTTYPE.SelectedValue != "" && ddlPAYMENTTYPE.SelectedValue != "0")
            {
                sSQL += " AND PP.PAYMENTTYPEID = @PAYMENTTYPEID";
                cmd.Parameters.AddWithValue("@PAYMENTTYPEID", ddlPAYMENTTYPE.SelectedValue);
            }
            if (rdpSTART.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND PP.PLANNEDPAYMENTDATE >= @EXPENSESTARTDATE";
                cmd.Parameters.AddWithValue("@EXPENSESTARTDATE", rdpSTART.SelectedDate);
            }
            if (rdpSTART.SelectedDate > Convert.ToDateTime("01/01/2007"))
            {
                sSQL += " AND PP.PLANNEDPAYMENTDATE <= @EXPENSEENDDATE";
                cmd.Parameters.AddWithValue("@EXPENSEENDDATE", rdpEND.SelectedDate);
            }

            sSQL += "    ORDER BY PLANNEDPAYMENTDATE DESC";

            cmd.CommandText = sSQL;

            DataTable dt = new DataTable();
            DbHelper dsrv = new DbHelper();

            dt = dsrv.GetDataTable(cmd, "TBL");
            return dt;
        }

        protected void rgMaster_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = GetData();
            rgMaster.DataSource = dt;

            rgMaster.MasterTableView.NoMasterRecordsText = "Kayıt bulunamadı.";
            rgMaster.StatusBarSettings.ReadyText = dt.Rows.Count + " adet kayıt bulundu.";
        }

        protected void rgMaster_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Pager)
            {
                Label lblRecordCount = new Label();
                e.Item.Controls[0].Controls.AddAt(0, lblRecordCount);
                lblRecordCount.Text = rgMaster.StatusBarSettings.ReadyText + " | sayfaya git : ";

                rgMaster.ShowStatusBar = rgMaster.PageCount <= 1 ? true : false;
            }
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView row = e.Item.DataItem as DataRowView;

                double amount = Convert.ToDouble(row["AMOUNT"]);
                string currencyCode = row["CURRENCYCODE"].ToString();

                Conversion Oc = new Conversion();
                e.Item.Cells[rgMaster.Columns.FindByUniqueName("AMOUNT").OrderIndex].Text = Oc.CNumS2C(amount) + " " + currencyCode;
            }
        }

        protected override void RaisePostBackEvent(IPostBackEventHandler source, String eventArgument)
        {
            base.RaisePostBackEvent(source, eventArgument);
            switch (eventArgument)
            {
                case "Rebind":
                    rgMaster.Rebind();
                    break;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rgMaster.Rebind();

        }
    }
}